/*
 * @describe 日志Dao文件
 * @author yisin
 * @date 2014-12-21 00:44:30
 */
package com.yisin.pbox.www.logs.dao;

import java.sql.SQLException;
import java.util.List;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.yisin.pbox.bean.pojo.system.Logs;
import com.yisin.pbox.frame.common.Response;
import com.yisin.pbox.frame.common.StatusCode;
import com.yisin.pbox.frame.log.ILogger;
import com.yisin.pbox.frame.log.LogFactory;
import com.yisin.pbox.www.logs.ILogsDao;

/**
 * <pre>
 * 日志 Dao
 * </pre>
 * 
 * @author yisin
 * @date 2014-12-21 00:44:30
 * 
 */
@Repository("logsDao")
public class LogsDao extends SqlSessionDaoSupport implements ILogsDao {
	private static ILogger logger = LogFactory
			.getDefaultLogger(LogsDao.class);

	/**
	 * <pre>
	 * 根据ID查询记录
	 * </pre>
	 * 
	 * @author yisin
	 * @date 2014-12-21 00:44:30
	 */
	public Response getDataById(int logId) throws SQLException {
		logger.debug("In getDataById");
		Response res = new Response();
		Logs logs = (Logs) getSqlSession().selectOne(
				"LogsMapper.getDataById", logId);
		if (logs != null) {
			res.setStatusCode(StatusCode.SUCCESS);
			res.setObject(logs);
		} else {
			res.setStatusCode(StatusCode.FAILED);
		}
		return res;
	}

	/**
	 * <pre>
	 * 加载全部 / 模糊搜索日志数据
	 * </pre>
	 * 
	 * @author yisin
	 * @date 2014-12-21 00:44:30
	 */
	public Response loadAndSearch(Logs logs) throws SQLException {
		logger.debug("In loadAndSearch");
		Response res = new Response();
		List<Logs> list = getSqlSession().selectList(
				"LogsMapper.loadAndSearch", logs);
		if (list != null) {
			res.setStatusCode(StatusCode.SUCCESS);
			res.setList(list);
		} else {
			res.setStatusCode(StatusCode.FAILED);
		}
		return res;
	}

	/**
	 * <pre>
	 * 高级搜索日志数据
	 * </pre>
	 * 
	 * @author yisin
	 * @date 2014-12-21 00:44:30
	 */
	public Response advancedSearch(Logs logs) throws SQLException {
		logger.debug("In advancedSearch");
		Response res = new Response();
		List<Logs> list = getSqlSession().selectList(
				"LogsMapper.advancedSearch", logs);
		if (list != null) {
			res.setStatusCode(StatusCode.SUCCESS);
			res.setList(list);
		} else {
			res.setStatusCode(StatusCode.FAILED);
		}
		return res;
	}
	
	/**
	 * <pre>
	 * 精确查询日志数据
	 * </pre>
	 * 
	 * @author yisin
	 * @date 2014-12-21 00:44:30
	 */
	public Response selectDataForAccurate(Logs logs) throws SQLException {
		logger.debug("In selectDataForAccurate");
		Response res = new Response();
		List<Logs> list = getSqlSession().selectList(
				"LogsMapper.selectDataForAccurate", logs);
		if (list != null) {
			res.setStatusCode(StatusCode.SUCCESS);
			res.setList(list);
		} else {
			res.setStatusCode(StatusCode.FAILED);
		}
		return res;
	}

	/**
	 * <pre>
	 * 新增日志记录
	 * </pre>
	 * 
	 * @author yisin
	 * @date 2014-12-21 00:44:30
	 */
	public Response insertData(Logs logs) throws SQLException {
		logger.debug("In insertData");
		Response res = new Response();
		int status = getSqlSession().insert("LogsMapper.insertData", logs);
		// 如果需要带出刚插入这条记录的ID
		// 则mapper中需要设置<insert id="insert" parameterType="Logs" useGeneratedKeys="true" keyProperty="logId">，然后从实体logId中获取
		if (status > 0) {
			res.setStatusCode(StatusCode.SUCCESS);
		} else {
			res.setStatusCode(StatusCode.FAILED);
		}
		return res;
	}

	/**
	 * <pre>
	 * 修改日志信息
	 * </pre>
	 * 
	 * @author yisin
	 * @date 2014-12-21 00:44:30
	 */
	public Response updateData(Logs logs) throws SQLException {
		logger.debug("In updateData");
		Response res = new Response();
		int status = getSqlSession().insert("LogsMapper.updateData", logs);
		if (status > 0) {
			res.setStatusCode(StatusCode.SUCCESS);
		} else {
			res.setStatusCode(StatusCode.FAILED);
		}
		return res;
	}

	/**
	 * <pre>
	 * 删除日志
	 * </pre>
	 * 
	 * @author yisin
	 * @date 2014-12-21 00:44:30
	 */
	public Response deleteData(Logs logs) throws SQLException {
		logger.debug("In deleteData");
		Response res = new Response();
		int status = getSqlSession().insert("LogsMapper.deleteData", logs);
		if (status > 0) {
			res.setStatusCode(StatusCode.SUCCESS);
		} else {
			res.setStatusCode(StatusCode.FAILED);
		}
		return res;
	}

	/**
	 * <pre>
	 * 审核日志
	 * </pre>
	 * 
	 * @author yisin
	 * @date 2014-12-21 00:44:30
	 */
	public Response auditingData(Logs logs) throws SQLException {
		logger.debug("In auditingData");
		Response res = new Response();
		int status = getSqlSession().insert("LogsMapper.auditingData", logs);
		if (status > 0) {
			res.setStatusCode(StatusCode.SUCCESS);
		} else {
			res.setStatusCode(StatusCode.FAILED);
		}
		return res;
	}
}
